﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using Meqs.Logic.Interface;
using Meqs.Logic.Core;

namespace Meqs.Logic.Implement.Oracle
{
    public class DbManagerLogic : BaseSqlMapDao, IDbManagerLogic
    {
        /// <summary>
        /// 创建各学校用户并授权
        /// </summary>
        /// <param name="schoolCode"></param>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        public bool CreateDbUser(string schoolCode, string folderPath)
        {
            //string xmlPath = HttpContext.Current.Server.MapPath("Config/GeneratorSchoolRule.xml");
            //XmlDocument doc = new XmlDocument();
            //doc.Load(xmlPath);
            //return GeneratorOperator(doc, schoolCode, folderPath);
            folderPath = folderPath.TrimEnd('/') + "/Oracle";
            return Generator(schoolCode, folderPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schoolCode"></param>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        private static bool Generator(string schoolCode, string folderPath)
        {
            OracleConnection conn = new OracleConnection(ConnectionString);
            try
            {
                conn.Open();
                DirectoryInfo di = new DirectoryInfo(folderPath);

                IList<string> list = new List<string>();
                list.Add(string.Format("CREATE USER {0} PROFILE \"DEFAULT\" IDENTIFIED BY \"123456\"", schoolCode));
                list.Add(string.Format("GRANT CONNECT TO {0}", schoolCode));
                list.Add(string.Format("GRANT RESOURCE TO {0}", schoolCode));
                list.Add(string.Format("GRANT UNLIMITED TABLESPACE TO {0}", schoolCode));

                foreach (string sql in list)
                {
                    OracleCommand cmd = conn.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }

                foreach (FileInfo info in di.GetFiles("*school*.sql"))
                {
                    try
                    {
                        StreamReader sr = new StreamReader(info.FullName);
                        string fileContent = sr.ReadToEnd();
                        sr.Close();
                        string[] sqlArr = fileContent.Split(';');
                        foreach (string sql in sqlArr)
                        {
                            if (sql.Trim() == "") continue;
                            string s = sql.Replace("CREATE TABLE ", "CREATE TABLE " + schoolCode + ".").Replace("CREATE SEQUENCE ", "CREATE SEQUENCE " + schoolCode + ".").Replace("\r", "").Replace("\n", "");

                            OracleCommand cmd = conn.CreateCommand();
                            cmd.CommandText = s;
                            cmd.CommandType = CommandType.Text;
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                }
                folderPath = folderPath.TrimEnd('\\');
                StreamReader sr1 = new StreamReader(folderPath + "\\score.init.sql", Encoding.UTF8);
                string fileContent1 = sr1.ReadToEnd();
                sr1.Close();
                string[] sqlArr1 = fileContent1.Split(';');
                foreach (string sql in sqlArr1)
                {
                    if (sql.Trim() == "") continue;
                    string s = string.Format(sql, schoolCode);

                    OracleCommand cmd = conn.CreateCommand();
                    cmd.CommandText = s;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
